/**
 * 
 */
package org.vectrics.recruiter.struts.cv;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Logger;
import org.vectrics.common.util.DateUtil;
import org.vectrics.cv.Experience;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

/**
 * Clase que implementa el editar de una experiencia laboral
 * @author maguero
 */
public class ShowExperienceEditAction extends VectricsStrutsAction {

	private transient static Logger log = Logger.getLogger(ShowExperienceEditAction.class);
	
	private ExperienceForm form;
	
	public ExperienceForm getForm() {
		return form;
	}

	public void setForm(ExperienceForm form) {
		this.form = form;
	}
	
	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		log.info("ShowExperienceEditAction.doExecute() called");
		
		// Verifico que el id de persona no sea nulo
		Request request = actionData.getRequest();
	    if (request.getParameter("personId")==null) {
		   	log.error("personId is null");
	    	throw new JspException("personId is null");
	    }
	    
	    // verifico que id de experiencie no sea nulo
	    Long experienceId = null;
	    if (request.getParameter("experienceId") != null) {
	    	//Recuperar el studiesId pasado por parametro 
	    	experienceId = new Long(request.getParameter("experienceId"));
	    } else {
	    	return "failure";
	    }
	    
	    this.setForm((ExperienceForm) actionData.getForm());
	    
	    // Recupero el experience con el id pasado por parametro.
		Experience experience= CoreServiceLocator.getPersonService().findExperienceIdById(experienceId);
		
		// Seteo los datos al formulario.
		this.getForm().setId(experience.getId());
		this.getForm().setPost(experience.getPost());
		this.getForm().setCompany(experience.getCompany());
		if (experience.getCompanyActivity() != null) {
			this.getForm().setCompanyActivity(experience.getCompanyActivity().getId().toString());
		} else {
			this.getForm().setCompanyActivity("");
		}
		this.getForm().setDateIncome(DateUtil.getFechaFormateada(experience.getDateIncome()));
		if (experience.getDateDischarge() != null) {
			this.getForm().setDateDischarge(DateUtil.getFechaFormateada(experience.getDateDischarge()));
			this.getForm().setWorkHere(false);
		} else {
			this.getForm().setDateDischarge(null);
			this.getForm().setWorkHere(true);
		}
		this.getForm().setWork(experience.getWork());
		
	    return "success";
	}

	
}
